const express=require("express");
const app = express();
const path=require("path");
const user=require("./user");
app.use("/user",user);
app.use("/static",express.static('static'));
app.use(express.urlencoded({extended:true}));
app.use(express.json());
app.use((req,res,next)=>{
  console.log("一个请求");
  next();
});
app.get('/index', (req, res) => {
    // res.send('index');
    res.sendFile(path.resolve("./public/index.html"));
  })
app.get('/login', (req, res) => {
    // res.send('login');
    res.sendFile(path.resolve("./public/login.html"));
  })
app.post('/login', (req, res) => {
    res.send('post');
  })
app.delete('/news', (req, res) => {
    res.send('删除成功');
  })
app.put('/user', (req, res) => {
    res.send('推送成功');
  })
app.patch('/edit', (req, res) => {
    res.send('编辑成功');
  })
app.all('/hello', (req, res) => {
    res.send('hello world');
  })
app.get('/random.text', (req, res) => {
  res.send('ZZZ');
})
// app.get('/ab?cd', (req, res) => {
//   res.send('ab?cd');//?代表1个或0个
// })
// app.get('/ab+cd', (req, res) => {
//   res.send('ab+cd');//+代表1个或多个
// })
app.get('/ab*cd', (req, res) => {
  res.send('ab*cd');//*代表中间任意个字符
})
app.get(/^\/\d{4}$/, function (req, res) {
  res.send('正则')
})
app.get('/users/:userId/books/:bookId', function (req, res) {
  res.send(req.params.bookId)
})
app.set('view engine', 'ejs');
app.set("views","./template")
app.get('/main', (req, res) => {
  // res.download('./demo.txt');
  // res.json([{name:"zzk"},{sex:"male"}])
  // res.send([{name:"zzk"},{sex:"male"}])
  // res.redirect("/index")
  // throw new Error("错误！")
  let arr=[{id:1,name:"zzk"},{id:2,name:"zzz"}];
  res.render("main",{data:arr});
})
app.get('/mul',(req,res,next)=>{
  console.log('the response will be sent by the next function ...');
  next();
},(req,res)=>{
  res.send("end");
})
app.post("/postData",(req,res)=>{
  console.log(req.body);
  res.send({code:200,msg:"添加成功"});
})
app.use(function (err, req, res, next) {
  console.error(err.stack)
  res.status(500).send('Something broke!')
})
app.listen(3000);
